package com.fj.cleardatajob;

import com.fj.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class UserClearDataJob {
    private static final Logger logger = LoggerFactory.getLogger(UserClearDataJob.class);

    @Resource
    private UserService userService;

    @Scheduled(cron = "0 */1 * * * ?")
    public void userClearDataJob(){
        logger.info("---------定时任务开始执行---------"+new SimpleDateFormat("HH:mm:ss").format(new Date()));
        clearData(userService);
        logger.info("---------定时任务执行成功---------"+new SimpleDateFormat("HH:mm:ss").format(new Date()));
    }

    /**
     * 清理数据
     * @param userService
     */
    private static void clearData(UserService userService) {
        try{
            userService.deleteHomeNewsData();//调用service层的方法 删除数据库数据
        }catch (Exception e){
            logger.error("清理数据失败，失败原因："+e.getMessage());
        }
    }
}
